package com.wjk.easy.ui;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.wjk.easy.R;
import com.wjk.easy.adapter.ExamManageListAdapter;
import com.wjk.easy.db.Exam;
import com.wjk.easy.db.LastUserBean;
import com.wjk.easy.utils.D;
import com.wjk.easy.view.CreateExamDialog;
import com.wjk.easy.view.RecycleViewDivider;

import org.litepal.crud.callback.SaveCallback;
import java.util.List;

public class ExamManageActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exam_manage_list);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mAdapter!=null&&mAdapter.getItemCount()>0){
                    setResult(200);
                }
                finish();//返回
            }
        });

        ViewGroup fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showAddDialog();
            }
        });
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        initView();
        initData();
    }

    RecyclerView listview;
    ExamManageListAdapter mAdapter;
    private void initView() {
        listview = findViewById(R.id.rv_list);
        // 定义一个线性布局管理器
        LinearLayoutManager manager = new LinearLayoutManager(this);
        // 设置布局管理器
        listview.setLayoutManager(manager);
        listview.addItemDecoration(new RecycleViewDivider(
                this, LinearLayoutManager.VERTICAL, 1, getResources().getColor(R.color.colorLightTrans)));
    }

    LastUserBean currentUser;
    private void initData(){
        currentUser = D.queryCurrentUser(this);
        if(currentUser!=null){
            List<Exam> examList = D.queryExamList(currentUser.getUserId());
            if(examList.size()<1){
                showNoData();
            }else{
                initAdapter(examList);
            }
        }
    }

    private void initAdapter(List<Exam> examList){
        mAdapter = new ExamManageListAdapter(this, examList);
        mAdapter.setEmptyListener(new ExamManageListAdapter.EmptyListener() {
            @Override
            public void onEmpty() {
                showNoData();
            }
            @Override
            public void onExportClick(Exam exam) {
            }
        });
        listview.setAdapter(mAdapter);
    }

    private void showNoData() {
        listview.setVisibility(View.GONE);
        findViewById(R.id.tv_no_data_tip).setVisibility(View.VISIBLE);
    }

    private void showHaveData() {
        listview.setVisibility(View.VISIBLE);
        findViewById(R.id.tv_no_data_tip).setVisibility(View.GONE);
    }

    private void showAddDialog() {
        final CreateExamDialog dialog = new CreateExamDialog(this, R.style.Theme_AppCompat_Light_Dialog_Alert, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final Exam data = (Exam) view.getTag();
                data.setUserId(currentUser.getUserId());
                D.saveData(data,new SaveCallback() {
                    @Override
                    public void onFinish(boolean success) {
                        Log.i("wjk",success+"");
                        if(success){
                            List<Exam> examList = D.queryExamList(currentUser.getUserId());
                            if(mAdapter!=null){
                                mAdapter.setDataList(examList);
                            }else{
                                initAdapter(examList);
                                showHaveData();
                            }
                        }else{
                            Toast.makeText(ExamManageActivity.this,"添加失败",Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });
        dialog.show();
    }


    @Override
    public void onBackPressed() {
        if(mAdapter!=null&&mAdapter.getItemCount()>0){
            setResult(200);
        }
        super.onBackPressed();
    }
}
